package com.cloudwise.controller;

import com.cloudwise.entity.ReturnBean;
import com.cloudwise.entity.Question;
import com.cloudwise.service.QuestionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 问题表(Question)表控制层
 *
 * @author
 * @since 2021-11-23 16:11:44
 */
@RestController
@RequestMapping("question")
@Slf4j//赛雷锋  for  java
public class QuestionController {
    /**
     * 服务对象
     */
    @Resource
    private QuestionService questionService;

    /**
     * 分页查询
     *
     * @param question 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Question>> queryByPage(Question question, PageRequest pageRequest) {
        return ResponseEntity.ok(this.questionService.queryByPage(question, pageRequest));
    }
    @GetMapping("queryByPageNew")
    public ReturnBean queryByPageNew(Question question, Integer page, Integer limit) {
        ReturnBean returnBean = new ReturnBean();
        //为了支持layui分页的参数，page,limit,构建新的pageRequest对象
        PageRequest pageRequest = PageRequest.of(page-1, limit);
        Page<Question> questions = questionService.queryByPage(question, pageRequest);
       //设置返回的数据
        returnBean.setData(questions.toList());
        //设置返回的状态码，0为正常
        returnBean.setCode(0);
    /*    log.debug("getTotalElements"+questions.getTotalElements());
        log.debug("getSize"+questions.getSize());
        log.debug("getNumber"+questions.getNumber());
        log.debug("getTotalPages"+questions.getTotalPages());*/
        //设置返回的总条数
        returnBean.setCount(questions.getTotalElements());
        return returnBean;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Question> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.questionService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param question 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Question> add(Question question) {
        return ResponseEntity.ok(this.questionService.insert(question));
    }

    /**
     * 编辑数据
     *
     * @param question 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Question> edit(Question question) {
        return ResponseEntity.ok(this.questionService.update(question));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.questionService.deleteById(id));
    }


    @RequestMapping("queryAllQuestion")
    public ReturnBean queryAllQuestion(){
        //log.info("开始查询所有的问题");
        log.trace("trace级别的日志");
        log.debug("debug开始查询所有的问题");
        log.info("info级别的日志");
        log.warn("warn级别的日志");
        log.error("error级别的日志");
       // System.out.println("fdsajfks");
        ReturnBean returnBean = new ReturnBean();
        List<Question> questionList = questionService.queryAllQuestion();
        returnBean.setCode(0);
        returnBean.setCount(30L);
        returnBean.setData(questionList);
        return returnBean;
    }

}

